<?php

namespace App\Http\Controllers;

use App\Collect;
use App\Msg;
use App\Question;
use App\Sign;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;

class UserController extends Controller
{
    //
    public function index(User $user)
    {

        return view('user.index',compact('user'));
    }
    public function set()
    {
        $user = \Auth::user();
        return view('user.set',compact('user'));
    }
    public function setStore(Request $request)
    {
        $user = \Auth::user();
        $this->validate(
            $request,
            [
                'email' => [
                    Rule::unique('users')->ignore($user->id),
                    'email',
                ],
                'name' => [
                    Rule::unique('users')->ignore($user->id),
                    'min:3',
                    'max:20',
                ],
                'password'=> 'max:20|min:3|confirmed',
                'city' => 'max:20|min:1',
                'sign' => 'min:3'
            ]
        );
        $data = $request->all();
        if($request->has('password')){
            $data['password'] = bcrypt($request->password);
            unset($data['password_confirmation']);
        }
        User::where('id',\Auth::id())
            ->update($data);
        return [
            'error'=>0,
            'info'=>'修改成功',
        ];
    }
    public function checkFace(Request $request)
    {
        if ($request->isMethod('post')) {

            $file = $request->file('file');

            // 文件是否上传成功
            if ($file->isValid()) {
                // 获取文件相关信息
                $ext = $file->getClientOriginalExtension();     // 扩展名
                $realPath = $file->getRealPath();   //临时文件的绝对路径
                // 上传文件
                $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
                // 使用我们新建的uploads本地存储空间（目录）
                $bool = Storage::disk('uploads')->put($filename, file_get_contents($realPath));
                if ($bool){
                    $face = '/uploads/'.$filename;
                    $user = \Auth::user();
                    $user->face = $face;
                    $user->save();
                    return[
                        'error'=>0,
                        'info' =>'更换头像成功',
                    ];
                }else{
                    return[
                        'error'=>1,
                        'info' =>'更换头像失败',
                    ];
                }
            }
        }
    }

    public function myQuestion()
    {
        $user = User::withCount('question')->withCount('collect')->find(\Auth::id());
        $questions = Question::where('user_id',\Auth::id())->orderBy('created_at','desc')->paginate(5);
        $collects = Collect::where('user_id',\Auth::id())->orderBy('created_at','desc')->paginate(5);
        return view('user.myquestion',compact('user','questions','collects'));
    }

    public function msg(User $user)
    {
        $msgs = $user->msg()->paginate(5);
        return view('user.msg',compact('msgs'));
    }

    public function clearMsg(Request $request)
    {
        $msg_id = $request->mid;
        Msg::destroy($msg_id);
        return[
          'error'=>0,
          'info'=>'删除成功',
        ];
    }

    public function clearAll()
    {
        Msg::where('target_id',\Auth::id())->delete();
        return[
            'error'=>0,
            'info'=>'删除成功',
        ];
    }

    public function sign()
    {
        $user = User::where('id',\Auth::id())->first();
        if(!$user->mysign){
            $sign = new Sign();
            $sign->user_id = \Auth::id();
            $sign->sign_num = 1;
            $sign->created_at = date('Y-m-d');
            $sign->save();
            return[
                'error'=>0,
                'info'=>'签到成功',
            ];
        }else{
            if(time()>strtotime($user->mysign->created_at)+86400){
                $sign = Sign::where('user_id',\Auth::id())->first();
                if (time()<strtotime($user->mysign->created_at)+86400*2){
                    $sign->sign_num ++;
                    $sign->created_at = date('Y-m-d');
                    $sign->save();
                    return[
                        'error'=>0,
                        'info'=>'签到成功',
                    ];
                }else{
                    $sign->sign_num = 1;
                    $sign->created_at = date('Y-m-d');
                    $sign->save();
                    return[
                        'error'=>0,
                        'info'=>'签到成功',
                    ];
                }
            }
            return[
                'error'=>1,
                'info'=>'今日已签到',
            ];
        }
    }
}
